<?php
namespace Secret\Twig\Extensions;

use Symfony\Component\DependencyInjection\ContainerInterface;
use \Twig_Extension;
use Secret\Common\Utils;

class Extension extends Twig_Extension {

	public function getName() {
		return 'some.extension';
	}

	public function getFilters() {
		return array(
			'json_decode' => new \Twig_Filter_Method($this, 'jsonDecode'),
			'ceil' => new \Twig_Filter_Method($this, 'ceil'),
			'pages' => new \Twig_Filter_Method($this, 'pages'),
			'nav' => new \Twig_Filter_Method($this, 'nav'),
			'nav2' => new \Twig_Filter_Method($this, 'nav2'),
			'langdata' => new \Twig_Filter_Method($this, 'langdata'),
			'langdata2' => new \Twig_Filter_Method($this, 'langdata2'),
			'imgsrc' => new \Twig_Filter_Method($this, 'imgsrc'),
			'num2word' => new \Twig_Filter_Method($this, 'num2word'),
			'hasVal' => new \Twig_Filter_Method($this, 'hasVal'),
			'myGlobal' => new \Twig_Filter_Method($this, 'myGlobal'),
		);
	}
	
	public function myGlobal($globals, $title) {
		foreach ($globals['_data'] as $itm) {
			if ($itm['title'] == $title) {
				return $itm['content'];
			}
		}
		return null;
	}

	public function jsonDecode($str) {
		return json_decode($str);
	}

	public function ceil($number) {
		return ceil($number);
	}

	public function pages($node) {
		if (count($node['_c']) == 0) {
			return '';
		}
		$str = '<ol class="dd-list">';
		foreach ($node['_c'] as $itm) {
			$str .= '<li class="dd-item dd3-item" data-id="' . $itm['id'] . '">';
			$str .= '<div class="dd-handle dd3-handle"></div>';
			$str .= '<div class="dd3-content">';
			$str .= '<span>' . $itm['title'] . '</span>';
			$str .= '<a href="/secret/contents/detail/Page/' . ($itm['slug'] != '' ? $itm['slug'] : urlencode($itm['title'])) . '/' . Utils::encodeURL(Utils::getURL()) . '/" class="edit"><i class="fa fa-pencil"></i></a>';
			$str .= '<a href="#" class="delete" data-id="' . $itm['id'] . '"><i class="fa fa-times"></i></a>';
			$str .= '</div>';
			$str .= $this->pages($itm);
			$str .= '</li">';
		}
		$str .= '</ol>';
		return $str;
	}
	
	public function nav($node) {
		if (count($node['_c']) == 0) {
			return '';
		}
		$str = '<ul>';
		foreach ($node['_c'] as $itm) {
			$str .= '<li>';
			$str .= '<a href="/' . ($itm['slug'] != '' ? $itm['slug'] : urlencode($itm['title'])) . '/">' . $itm['title'] . '</a>';
			$str .= $this->nav($itm);
			$str .= '</li">';
		}
		$str .= '</ul>';
		return $str;
	}
	
	public function nav2($node, $lang) {
		if (count($node['_c']) == 0) {
			return '';
		}
		$str = '<ul>';
		foreach ($node['_c'] as $itm) {
			if (isset($itm['homepage']) && $itm['homepage'] == 1) {
				continue;
			}
			$labels = json_decode($itm['labels']);
			// 			Utils::dump($labels[0]->blocks);exit;
			$str .= '<li ' . ((isset($itm['className']) && $itm['className'] != '') ? 'class="' . $itm['className'] . '"' : '') . '>';
			$str .= '<a href="/' . $lang . '/' . ($itm['slug'] != '' ? $itm['slug'] : urlencode($itm['title'])) . '/">' . (isset($labels[0]->blocks[0]->$lang) ? strip_tags($labels[0]->blocks[0]->$lang) : '') . '</a>';
			$str .= $this->nav($itm, $lang);
			$str .= '</li">';
		}
		$str .= '</ul>';
		return $str;
	}
	
	public function langdata($content, $lang) {
		$content = json_decode($content);
		if ($content && gettype($content) == 'array') return isset($content[0]->blocks[0]->$lang) ? strip_tags($content[0]->blocks[0]->$lang) : '';
		else if ($content && gettype($content) == 'object') return isset($content->blocks[0]->$lang) ? strip_tags($content->blocks[0]->$lang) : '';
		return '';
	}
	
	public function langdata2($content, $lang) {
		$content = json_decode($content);
		if ($content && gettype($content) == 'array') return isset($content[0]->blocks[0]->$lang) ? ($content[0]->blocks[0]->$lang) : '';
		else if ($content && gettype($content) == 'object') return isset($content->blocks[0]->$lang) ? ($content->blocks[0]->$lang) : '';
		return '';
	}
	
	public function imgsrc($html) {
		preg_match_all('/<img[^>]+>/i',$html, $result);
		preg_match_all('/src=("[^"]*")/i',$result[0][0], $img);
		return str_replace('"', '', $img[1][0]);
	}
	
	public function num2word($num) {
		$words = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten');
		return $words[$num - 1];
	}
	
	public function hasVal($arr, $val) {
		$arr = json_decode($arr);
		if (!$arr) {
			$arr = array();
		}
		return in_array($val, $arr);
	}
}